﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Achievement.DataModels;
using Achievement.Common;
using System.Data.Objects;
using System.Web;

namespace Achievement.BLL.Provider
{
    public class AchievementsProvider
    {
        private static readonly AchievementsProvider instance = new AchievementsProvider();
        public static AchievementsProvider Instance
        {
            get { return instance; }
        }
        private AchievementsProvider() { }

        public List<Folder> GetFolders()
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Folder.ToList();
            }
        }
        public List<Achievements> GetAchievementsList(string where, long folder,string type, int page, int rows, string sord, string sidx, out int totalRecords)
        {
            string sql = @"SELECT * FROM [Achievements] AS [a] WHERE 1 = 1";

            //var query = CGK.Achievements.Where(u => u.ProjectName.Contains("防"));
            if (!string.IsNullOrEmpty(where))
            {
                sql += where;
                //query = query.Where(u => u.ProjectName.Contains(where));
                //string abc = ((ObjectQuery)query).ToTraceString();
            }
            if (folder > 0)
            {
                sql += " and Folder=" + folder;
            }
            if (!string.IsNullOrEmpty(type))
            {
                sql += " and type='" + type + "'";
            }
            using (CGKEntities CGK = new CGKEntities())
            {
                var query = CGK.ExecuteStoreQuery<Achievements>(sql).AsQueryable();
                totalRecords = query.Count();
            }
            using (CGKEntities CGK = new CGKEntities())
            {
                var query = CGK.ExecuteStoreQuery<Achievements>(sql).AsQueryable();
                var list = DBHelper.SortingAndPaging<Achievements>(query, sidx, sord, page - 1, rows).ToList();
                return list;
            }
        }

        public List<Achievements> GetAchievementsListTop(int count)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Achievements.OrderByDescending(a=>a.ID).Take(count).ToList();
            }
        }

        public Folder GetFolderById(long id)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Folder.Where(f=>f.ID == id).FirstOrDefault();
            }
        }

        public List<Folder> GetFolderList()
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Folder.ToList();
            }
        }

        public int SaveAchievement(Achievements acmt)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                CGK.Achievements.Attach(acmt);
                CGK.ObjectStateManager.ChangeObjectState(acmt, System.Data.EntityState.Modified);
                return CGK.SaveChanges();
            }
        }

        public Achievements GetAchievementById(long id)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Achievements.Where(a => a.ID == id).FirstOrDefault();
            } 
        }

        public List<Achievements> GetAchievementByIds(long[] ids)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Achievements.Where(a => ids.Contains(a.ID)).ToList();
            }
        }

        public void UpdateCount(long id)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                Achievements acm = CGK.Achievements.Where(a => a.ID == id).FirstOrDefault();
                if (acm != null)
                {
                    acm.Count++;
                    CGK.SaveChanges();
                }
            } 
        }

        public void AddAchievement(Achievements acmt)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                CGK.Achievements.AddObject(acmt);
                CGK.SaveChanges();
            } 
        }

        public List<Comment> GetCommentList(int page, int rows, string sord, string sidx, out int totalRecords)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var query = CGK.Comment.Where(c =>1 == 1);
                var list = DBHelper.SortingAndPaging<Comment>(query, sidx, sord, page - 1, rows).ToList();
                totalRecords = list.Count;
                return list;
            }
        }

        public List<VComment> GetVCommentList(long id, int page, int rows, string sord, string sidx, out int totalRecords)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var query = CGK.VComment.Where(c => c.AID == id);
                var list = DBHelper.SortingAndPaging<VComment>(query, sidx, sord, page - 1, rows).ToList();
                totalRecords = list.Count;
                return list;
            }
        }

        public Comment GetCommentById(long id)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Comment.Where(c => c.CID == id).FirstOrDefault();
            }
        }

        public int AddComment(Comment c)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                CGK.Comment.AddObject(c);
                return CGK.SaveChanges();
            }
        }

        public int EidtComment(Comment c)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                CGK.Comment.Attach(c);
                CGK.ObjectStateManager.ChangeObjectState(c, System.Data.EntityState.Modified);
                return CGK.SaveChanges();
            }
        }

        public void DeleteComment(long id)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                Comment comment = CGK.Comment.Where(c => c.CID == id).FirstOrDefault();
                CGK.Comment.DeleteObject(comment);
                CGK.SaveChanges();
            }
        }

        public void DeleteAchievementById(long id)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var query = CGK.Achievements.Where(a => a.ID == id).FirstOrDefault();
                if (query != null)
                {
                    CGK.Achievements.DeleteObject(query);
                    CGK.SaveChanges();
                }
            }
        }

        public List<VComment_A_U> GetCAUTop(int count)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.VComment_A_U.OrderByDescending(o => o.CreateTime).Take(count).ToList();
            }
        }

        public void AchievementAuditing(long aid, bool auditing)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var am = CGK.Achievements.Where(a => a.ID == aid).FirstOrDefault();
                if (am != null)
                {
                    am.IsActive = auditing;
                }
                CGK.SaveChanges();
            }
        }
    }
}
